package com.example.newsdemo;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView tvTop;
    private TextView tvYuLe;
    private TextView tvTiYu;
    private TextView tvKeJi;
    private TextView tvCaiJing;
    private ViewPager myViewPager;
    private List<Fragment> list;
    private TabFragmentPagerAdapter adapter;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();

        tvTop.setOnClickListener(this);
        tvYuLe.setOnClickListener(this);
        tvTiYu.setOnClickListener(this);
        tvKeJi.setOnClickListener(this);
        tvCaiJing.setOnClickListener(this);
        myViewPager.setOnPageChangeListener(new MyPagerChangeListener());

        list = new ArrayList<>();
        list.add(new TopFragment());
        list.add(new yuleFragment());
        list.add(new TiYuFragment());
        list.add(new KeJiFragment());
        list.add(new CaiJingFragment());
        adapter = new TabFragmentPagerAdapter(getSupportFragmentManager(),list);
        myViewPager.setAdapter(adapter);
        myViewPager.setCurrentItem(0);
        tvTop.setBackgroundColor(Color.RED);
    }
    private void initView() {
        tvTop = findViewById(R.id.tv_top);
        tvYuLe = findViewById(R.id.tv_yule);
        tvTiYu = findViewById(R.id.tv_tiyu);
        tvKeJi = findViewById(R.id.tv_keji);
        tvCaiJing = findViewById(R.id.tv_caijing);
        myViewPager = findViewById(R.id.myViewPager);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_top:
                myViewPager.setCurrentItem(0);
                tvTop.setBackgroundColor(Color.RED);
                break;
            case R.id.tv_yule:
                myViewPager.setCurrentItem(1);
                tvYuLe.setBackgroundColor(Color.RED);
                break;
            case R.id.tv_tiyu:
                myViewPager.setCurrentItem(2);
                tvTiYu.setBackgroundColor(Color.RED);
                break;
            case R.id.tv_keji:
                myViewPager.setCurrentItem(3);
                tvKeJi.setBackgroundColor(Color.RED);
                break;
            case R.id.tv_caijing:
                myViewPager.setCurrentItem(4);
                tvCaiJing.setBackgroundColor(Color.RED);
                break;
        }

    }

    public class MyPagerChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            switch (position) {
                case 0:
                    tvTop.setBackgroundColor(Color.RED);
                    tvYuLe.setBackgroundColor(Color.WHITE);
                    tvTiYu.setBackgroundColor(Color.WHITE);
                    tvKeJi.setBackgroundColor(Color.WHITE);
                    tvCaiJing.setBackgroundColor(Color.WHITE);
                    break;
                case 1:
                    tvTop.setBackgroundColor(Color.WHITE);
                    tvYuLe.setBackgroundColor(Color.RED);
                    tvTiYu.setBackgroundColor(Color.WHITE);
                    tvKeJi.setBackgroundColor(Color.WHITE);
                    tvCaiJing.setBackgroundColor(Color.WHITE);

                    break;
                case 2:
                    tvTop.setBackgroundColor(Color.WHITE);
                    tvYuLe.setBackgroundColor(Color.WHITE);
                    tvTiYu.setBackgroundColor(Color.RED);
                    tvKeJi.setBackgroundColor(Color.WHITE);
                    tvCaiJing.setBackgroundColor(Color.WHITE);

                    break;
                case 3:
                    tvTop.setBackgroundColor(Color.WHITE);
                    tvYuLe.setBackgroundColor(Color.WHITE);
                    tvTiYu.setBackgroundColor(Color.WHITE);
                    tvKeJi.setBackgroundColor(Color.RED);
                    tvCaiJing.setBackgroundColor(Color.WHITE);

                    break;
                case 4:
                    tvTop.setBackgroundColor(Color.WHITE);
                    tvYuLe.setBackgroundColor(Color.WHITE);
                    tvTiYu.setBackgroundColor(Color.WHITE);
                    tvKeJi.setBackgroundColor(Color.WHITE);
                    tvCaiJing.setBackgroundColor(Color.RED);

                    break;
            }

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }


}
